package db

import (
	"WebApplication/model"
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

type OrderDatabase struct {
	Database *gorm.DB
	Transaction *gorm.DB
}



func ConnectToDB() *OrderDatabase{
	db, err := gorm.Open("mysql","root:123456@/test_db")
	if err != nil{
		panic(err)
	}

	fmt.Println("database is opened")

	err = db.DB().Ping()
	if err != nil{
		panic(err)
	}

	// 自动整合
	db.AutoMigrate(&model.Order{})
	fmt.Println("migrate successful...")

	var orderdatabase OrderDatabase
	orderdatabase.Database = db
	return &orderdatabase
}

func ConnectToDBSimple() *gorm.DB{
	db, err := gorm.Open("mysql","root:123456@/test_db")
	if err != nil{
		panic(err)
	}

	fmt.Println("database is opened")

	err = db.DB().Ping()
	if err != nil{
		panic(err)
	}

	// 自动整合
	db.AutoMigrate(&model.Order{})
	fmt.Println("migrate successful...")

	return db
}